#ifndef _MODULE_FIGHT
#define _MODULE_FIGHT
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "console.module.js"
#include "usolib.lib.js"
#include "utility.lib.js"
#include "ajax.lib.js"
#include "cache.lib.js"
#include "task.lib.js"

Modules.push( "Fight" );
var Fight = {
	running: false,
	list: false,
	status: "",
	count: 0,
	list_rank: 0,
	ignore: [],
	avoid: [],
	individual_count: 0,
	last_reset: 0,
	user: null,
	log: function(action) {
		var log = [{
			name: 'Script',
			value: 'Fight'
		},
		{
			name: 'Action',
			value: action
		}];
		Console.log(log);
		Console.status('fight', action);
	},
	init: function(resume) {
		if (resume) {
			if (Fight.running === false) {
				return;
			}
		}
		Fight.running = true;
		Fight.list = false;
		Fight.count = 0;
		Fight.user;
		Fight.list_rank = 0;
		Fight.ignore = Boss.ignore.concat(Fight.avoid);
		Fight.individual_count = 0;
		Fight.last_reset = 0;
		Fight.status = "LIST";
		Fight.log('Initiating Fight Automation');
		Ajax(Page.domain + '/mobwars/fight/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Fight.init(true);
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Fight.update_list(xhr.responseText);
			Fight.stats_check();
			return;
		});
		return;
	},
	counter: function() {
		Fight.count++;
		if (Fight.count >= Settings.script_repeat && Settings.script_repeat != -1) {
			Page.toggle('fight');
			return;
		}
		if ((Fight.count - Fight.last_reset) >= Settings.fight_reset && Settings.fight_reset != 0) {
			Fight.ignore = Boss.ignore.concat(Fight.avoid);
			Fight.last_reset = Fight.count;
		}
		return;
	},
	ignore_check: function(user_id) {
		for (var i = 0, il = Fight.ignore.length; i < il; i++) {
			if (user_id == Fight.ignore[i])
				return true;
		}
		return false;
	},
	update_list: function(html) {
		Fight.log('Updating Fight List');
		var doc = document.createElement('div');
		Fight.list = [];
		doc.innerHTML = html;
		$("//tr[contains(.,', Level')]", doc).each(function() {
			Fight.list.push({
				mob_count: parseInt(this.innerHTML.match(/<td>([0-9]*?)<\/td>/)[1]),
				level: parseInt(this.innerHTML.match(new RegExp('Level ([0-9].*?) (Bulletproof|Tycoon|Insomniac)'))[1]),
				user_id: $("//input[@name='target_id']", this).attr('value')
			});
		});
		if (Fight.list.length <= 0)
			Fight.list = false;
	},
	stats_check: function() {
		if (Fight.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Fight.stats_check();
				return;
			});
			return;
		};
		Fight.log('Checking Boss\'s Stats');
		var updating = false;
		if (Boss.cash >= Settings.bank_limit && Settings.bank && Cache.banking === false) {
			Fight.log('Banking ' + Utility.int_dollars(Boss.cash));
			Task.bank(Fight.stats_check);
			updating = true;
		}
		if (Settings.fight_stamina_stop >= Boss.stamina) {
			Fight.log('Recharging Boss\'s Stamina');
			var timer = new Utility.timer({
				script: 'fight',
				message: 'Waiting [[time]] for Stamina to recharge...',
				time: stamina_wait * (Settings.fight_stamina_start - Boss.stamina),
				fn: function() {
					Ajax(Page.domain + '/mobwars/fight/', function(xhr) {
						Page.captcha(xhr.responseText);
						if (Fight.running === false) {
							return;
						};
						if (Page.pausing === true) {
							Page.pause_cb(function() {
								Fight.stats_check();
								return;
							});
							return;
						};
						Boss.update(xhr.responseText);
						Fight.update_list(xhr.responseText);
						Fight.stats_check();
						return;
					});
				}
			});
			timer.start();
			updating = true;
		} else if (Boss.health <= Settings.min_health && Settings.heal && Cache.healing === false) {
			Fight.log('Healing your Boss');
			Task.heal(Fight.stats_check);
			updating = true;
		}
		if (updating) {
			Fight.list = false;
			return;
		}
		Fight.log('Stat checks complete');
		Fight.load_attack();
		return;
	},
	load_attack: function() {
		if (Fight.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Fight.stats_check();
				return;
			});
			return;
		};
		if (Fight.list === false) {
			Fight.list_rank = 0;
			Fight.log('Getting Fight List');
			Ajax(Page.domain + '/mobwars/fight/', function(xhr) {
				Page.captcha(xhr.responseText);
				if (Fight.running === false) {
					return;
				};
				if (Page.pausing === true) {
					Page.pause_cb(function() {
						Fight.stats_check();
						return;
					});
					return;
				};
				Boss.update(xhr.responseText);
				Fight.update_list(xhr.responseText);
				Fight.stats_check();
				return;
			});
			return;
		}
		if (Fight.status == "INDIVIDUAL") {
			Fight.individual_count++;
			if (Settings.repeat_limit > Fight.individual_count || Settings.repeat_limit == -1) {
				var timer = new Utility.timer({
					time: Page.wait(),
					message: 'Fighting individual Mob [[time]]...',
					script: 'fight',
					fn: function() {
						Fight.attack(Fight.user);
						return;
					}
				});
				timer.start();
				return;
			}
			Fight.ignore.push(Fight.user);
			Fight.individual_count = 0;
			Fight.status = "LIST";
		}
		var mob;
		while (Fight.list_rank < Fight.list.length) {
			level = true;
			mob = Fight.list[Fight.list_rank];
			if (Settings.fight_max_level > 0) {
				if (mob.level >= Settings.fight_max_level) {
					level = false;
				}
			}
			if (Settings.min_mob_size <= mob.mob_count && Settings.max_mob_size >= mob.mob_count && level) {
				if (Fight.ignore_check(mob.user_id) === false) {
					Fight.user = mob.user_id;
					var timer = new Utility.timer({
						time: Page.wait(),
						message: 'Fighting Mob [[time]]...',
						script: 'fight',
						fn: function() {
							Fight.attack(Fight.user);
							return;
						}
					});
					timer.start();
					Fight.list_rank++;
					return;
				}
			}
			Fight.list_rank++;
		}
		Fight.list_rank = 0;
		Fight.list = false;
		var timer = new Utility.timer({
			time: 180,
			message: 'Waiting for new Fight List [[time]]...',
			script: 'fight',
			fn: function() {
				Fight.stats_check();
				return;
			}
		});
		timer.start();
		return;
	},
	attack: function(user_id) {
		if (Fight.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Fight.stats_check();
				return;
			});
			return;
		};
		if (Settings.fight_punch != 'off') {
			Fight.log('Punching Mob ' + user_id);
			Ajax(Page.domain + '/mobwars/fight/do.php?action=punch&target_id=' + user_id + '&from=/profile/?user_id=' + user_id, function(xhr) {
				Page.captcha(xhr.responseText);
				if (Fight.running === false) {
					return;
				};
				if (Page.pausing === true) {
					Page.pause_cb(function() {
						Fight.stats_check();
						return;
					});
					return;
				};
				Boss.update(xhr.responseText);
				Fight.update_list(xhr.responseText);
				var ann = Task.announce(xhr.responseText);
				if (Settings.fight_punch == 'always') {
					Fight.status = "LIST";
					Fight.individual_count = 0;
					Fight.counter();
					Fight.stats_check();
				} else {
					Fight.fight(Fight.user);
				}
				return;
			});
		} else Fight.fight(user_id);
		return;
	},
	fight: function(user_id) {
		if (Fight.status == "INDIVIDUAL") Fight.log("Fighting Individual Mob " + user_id);
		else Fight.log("Fighting Mob " + user_id);
		Ajax(Page.domain + '/mobwars/fight/do.php?force=1&action=attack&target_id=' + user_id, function(xhr) {
			Page.captcha(xhr.responseText);
			if (Fight.running === false) {
				return;
			};
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Fight.stats_check();
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Fight.update_list(xhr.responseText);
			var ann = Task.announce(xhr.responseText);
			Fight.announce(ann);
			Fight.counter();
			Fight.stats_check();
		},
		'POST');
		return;
	},
	announce: function(ann) {
		if (ann) {
			if (ann.won) {
				Fight.log('Fight Won against ' + Fight.user + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager + ', gained ' + ann.cash_str + ' and ' + ann.exp + ' exp');
				if (Settings.repeat_type == "exp" && ann.exp >= Settings.repeat_threshold) {
					Fight.list = false;
					Fight.status = "INDIVIDUAL";
				} else if (Settings.repeat_type == "cash" && ann.cash >= Settings.repeat_threshold) {
					Fight.status = "INDIVIDUAL";
					Fight.list = false;
				}
			} else if (ann.lost) {
				Fight.individual_count = 0;
				Fight.status = "LIST";
				Fight.ignore.push(Fight.user);
				Fight.log('Fight Lost against ' + Fight.user + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager);
			} else if (ann) {
				Fight.individual_count = 0;
				Fight.status = "LIST";
				Fight.log('Fight Attack failed against ' + Fight.user + '! "' + ann.message + '"');
			}
		}
		return;
	}
};
#endif
